<template>
  <div class="page">
    <el-form :model="searchForm" label-position="left" inline label-suffix="：" size="small">
      <div class="flex wap flex-start">
        <el-form-item label="名称">
          <el-input v-model="searchForm.name" clearable></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-button type="primary" @click="getData">查询</el-button>
        </el-form-item>
      </div>
    </el-form>
    <div class="flex wap flex-start" style="padding: 0px 0 10px 0">
      <el-button type="success" @click="addData">新增</el-button>
      <div style="width:20px"></div>
      <!-- <el-button type="danger" @click="del">删除</el-button> -->
    </div>
    <el-table :data="tableData" border style="width: 100%" height="600" :header-cell-style="{
        background: '#eef1f6',
        color: '#606266',
        'text-align': 'center'
      }"
      :cell-style="{ 'text-align': 'center' }">
      <!-- <el-table-column type="selection"></el-table-column> -->
      <el-table-column prop="classname" label="班级名称"></el-table-column>
      <el-table-column prop="classinfo" label="班级信息" show-overflow-tooltip></el-table-column>
      <el-table-column prop="empid" label="班主任"></el-table-column>
      <el-table-column prop="createtime" label="创建时间"></el-table-column>
      <el-table-column prop="updatetime" label="更新时间"></el-table-column>
      <!-- <el-table-column prop="mainimg" label="班级头像">
        <template slot-scope="scop">
          <el-image fit="fil" style="width: 80px; height: 50px" :src="$Url + '/upload/' + scop.row.img"
            :preview-src-list="[$Url + '/upload/' + scop.row.img]">
          </el-image>
        </template>
      </el-table-column> -->
      <el-table-column prop="" label="操作" width="180">
        <template slot-scope="scop">
          <el-button type="primary" plain icon="el-icon-edit" circle @click="edit(scop.row)"></el-button>
          <el-button type="success" plain icon="el-icon-setting" circle @click="set(scop.row)"></el-button>
          <el-button type="danger" plain icon="el-icon-delete" circle @click="del(scop.row.classid)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="float:right;padding:10px">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 20, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog title="班级信息" center :visible.sync="dialogFormVisible" width="60%">
      <el-form :model="addOrUpdateForm" ref="addSetForm" :rules="addrule" label-width="130px" class="edit-form"
        label-suffix="：">
        <el-form-item label="班级名称" prop="classname">
          <el-input type="text" placeholder="请输入班级名称" v-model="addOrUpdateForm.classname"></el-input>
        </el-form-item>
        <el-form-item label="班级信息" prop="classinfo">
          <el-input type="text" placeholder="请输入班级信息" v-model="addOrUpdateForm.classinfo"></el-input>
        </el-form-item>
        <el-form-item label="班主任" prop="empid">
          <check-emp :itemId="addOrUpdateForm.empid" @checkedItem="addCheckedEmp" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSave" :loading="saveLoading">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="班级配置" center :visible.sync="settingDialog" width="70%">
      <h2>{{setClassName}}</h2>
      <el-form :model="setItemForm" :inline="true" ref="setForm" :rules="setRule" label-width="130px" class="edit-form"
        label-suffix="：">
        <el-form-item label="科目" prop="lessionid">

          <check-lession :itemId="setItemForm.lessionid" @checkedObj="checkedLession" />
        </el-form-item>
        <el-form-item label="教师" prop="empid">
          <check-emp :itemId="setItemForm.empid" @checkedObj="checkedEmp" />
        </el-form-item>
        <el-form-item label="">
          <el-button type="success" plain @click="addsetItem" style="margin-left: 30px">添加</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="settingDate" max-height="400" :header-cell-style="{
        background: '#eef1f6',
        color: '#606266',
        'text-align': 'center'
      }"
        :cell-style="{ 'text-align': 'center' }">
        <el-table-column type="index" label="编号">
        </el-table-column>
        <el-table-column prop="lessionname" label="科目">
        </el-table-column>
        <el-table-column prop="empname" label="教师">
        </el-table-column>
        <el-table-column prop="" label="操作" width="100">
          <template slot-scope="scop">
            <el-button type="danger" plain icon="el-icon-delete" circle @click="delset(scop.$index)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="settingDialog = false">返回</el-button>
        <el-button type="primary" @click="setSave" :loading="setLoading">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { findClass, updateClass, addClass, delClass, findLession, findEmp, updateConfiguration, findConfiguration } from "@/api/user";
import upload from "@/components/uploadFile";
import checkEmp from "@/views/components/checkEmp";
import checkLession from "@/views/components/checkLession";
import { log } from 'util';
export default {
  components: {
    upload,
    checkEmp,
    checkLession
  },
  data() {
    return {
      total: 0,
      pageNum: 1,
      pageSize: 10,
      tableData: [],
      dialogFormVisible: false,
      settingDialog: false,
      setLoading: false,
      saveLoading: false,
      searchForm: {
        name: "",
      },
      addOrUpdateForm: {
        classid: '',
        classname: '',
        classinfo: '',
        empid: '',
      },
      settingDate: [],
      addSettinglist: [],
      setClassName: '',
      setItemForm: {
        classid: '',
        lessionid: '',
        empid: '',
        lessionname: '',
        empname: ''
      },
      setRule: {
        lessionid: [
          { required: true, message: '必须选择科目', trigger: 'blur' }
        ],
        empid: [
          { required: true, message: '必须选择教师', trigger: 'blur' }
        ]
      },
      addrule: {
        classname: [
          { required: true, message: '必须填写名称', trigger: 'change' }
        ],
        classinfo: [
          { required: true, message: '必须填写班级简介', trigger: 'change' }
        ],
        empid: [
          { required: true, message: '必须选择班主任', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    queryData() {
      return {
        classname: this.searchForm.name,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    async getData() {
      const res = await findClass.post(this.queryData);
      if (res.resultCode === "000000") {
        this.tableData = res.data.classList;
        this.total = res.data.count;
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    async getSettingDate() {
      const res = await findConfiguration.post({ classid: this.setItemForm.classid })
      if (res.resultCode === "000000") {
        this.settingDate = res.data
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    addData() {
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['addSetForm'].resetFields();
      })
      this.addOrUpdateForm = {
        classid: '',
        classname: '',
        classinfo: '',
        empid: '',
      }
    },
    async set(row) {
      this.settingDialog = true
      const { classid, classname } = row
      this.setItemForm.classid = classid
      this.setItemForm.lessionid = ''
      this.setItemForm.empid = ''
      this.setClassName = classname
      this.getSettingDate()
    },
    delset(val) {
      console.log(val, 5);
      this.settingDate = this.settingDate.filter((e,i)=>i!=val)
    },
    addsetItem() {
      this.$refs.setForm.validate(async (valid) => {
        if (valid) {
          this.settingDate.unshift(JSON.parse(JSON.stringify(this.setItemForm)))
          
        }
      })
    },
    async setSave() {
      const data = this.settingDate.map(el => {
        return {
          lessionid: el.lessionid,
          empid: el.empid
        }
      })
      const res = await updateConfiguration.post({ empClassLessionList: data, classid: this.setItemForm.classid })
      if (res.resultCode === "000000") {
        this.$message.success(res.resultInfo);
        this.settingDialog = false
        this.setItemForm.lessionid = ''
        this.setItemForm.empid = ''
      } else {
        this.$message.error(res.resultInfo);
      }
    },

    async edit(row) {
      this.dialogFormVisible = true
      const { classid, classname, classinfo, empid } = row
      this.addOrUpdateForm = {
        classid,
        classname,
        classinfo,
        empid
      }
    },
    checkedLession(val) {
      console.log(val, 5565);

      this.setItemForm.lessionid = val.lessionid
      this.setItemForm.lessionname = val.lessionname
    },
    checkedEmp(val) {
      this.setItemForm.empid = val.empid
      this.setItemForm.empname = val.empname
    },
    addCheckedEmp(val) {
      this.addOrUpdateForm.empid = val
    },
    addSave() {
      this.$refs.addSetForm.validate(async (valid) => {
        if (valid) {
          this.saveLoading = true
          const res = this.addOrUpdateForm.classid ? await updateClass.post(this.addOrUpdateForm) : await addClass.post(this.addOrUpdateForm);
          this.saveLoading = false
          if (res.resultCode === "000000") {
            this.$message.success(res.resultInfo);
            this.dialogFormVisible = false
            this.getData()
          } else {
            this.$message.error(res.resultInfo);
          }
        } else {
          return false;
        }
      });
    },
    del(id) {
      this.$confirm("此操作将删除该项目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.delItem(id)
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async delItem(id) {
      const res = await delClass.post({ classid: id });
      if (res.resultCode === "000000") {
        this.$message.success(res.resultInfo);
        this.getData()
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    handleSizeChange(val) {
      this.pageNum = 1;
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getData();
    }
  }
};
</script>
<style lang="less" scoped></style>
